package com.compty.paint.web.front;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.compty.core.page.Page;
import com.compty.core.struts2.BaseAction;
import com.compty.paint.domain.Advertisement;
import com.compty.paint.domain.Painter;
import com.compty.paint.domain.Painting;
import com.compty.paint.domain.support.MarketIndexEntity;
import com.compty.paint.manager.AdvertisementManager;
import com.compty.paint.manager.PainterManager;
import com.compty.paint.manager.PaintingManager;

/**
 * @ToDO: 前端展示主页
 * @author zhuqh
 */
public class IndexAction extends BaseAction<Painting> {

	private static final long serialVersionUID = -6328604745438306928L;
	
    private PaintingManager paintingManager;
    private PainterManager painterManager;
    private AdvertisementManager advertisementManager;
    
 // ====================================================
    private List<Advertisement> advertisementList; /*** 广告集合 ****/
    
    @SuppressWarnings("unchecked")
	public String list() throws Exception {
    	
        Page page  = paintingManager.findPaintingsByPageOfPainterStarByRandom(null,1,12);//鉴赏
        List paintingListOfPainterStar  = (List) page.getResult();
        request.setAttribute("paintingListOfPainterStar", paintingListOfPainterStar);
        
        page = paintingManager.findAvailablePaintingsByPage(1, 12, "3");//瓷器
        List ciyi =  (List) page.getResult();
        request.setAttribute("ciyi", ciyi);
        
        page = paintingManager.findAvailablePaintingsByPage(1, 12, "4");//家具
        List jiaju =  (List) page.getResult();
        request.setAttribute("jiaju", jiaju);
        
        page = paintingManager.findAvailablePaintingsSortOfPainterByPage(1,12,null);//绘画
        List huihua =  (List) page.getResult();
        request.setAttribute("huihua", huihua);
        
        page = advertisementManager.findByPage(curPage,3,"1");
        this.advertisementList = (List<Advertisement>) page.getResult();
        
        return SUCCESS;
    }
    
    /**
     * 行情指数查看
     */
    public String viewMarketIndex(){
    	
       String type = request.getParameter("type");
       
	   Comparator<Painter> ascComparator = new PainterComparator(); 
	   Comparator<Painter> descComparator = Collections.reverseOrder(ascComparator); 
	   
	   List<Painter> painterList = null;
 	   if("guohua".equals(type)){
 		  painterList = painterManager.findGuohuaPainerStar();
 		 request.setAttribute("catalog_type", type);
	   }
	   else if("youhua".equals(type)){
		   painterList = painterManager.findyouhuaPainerStar();
		   request.setAttribute("catalog_type", "youhua");
	   }
	   else{
		   painterList = painterManager.findAllPainerStar();
		   request.setAttribute("catalog_type", "youhua");
	   }
 	   Collections.sort(painterList, descComparator);
      
 	   List<MarketIndexEntity> marketIndex = getMarketIndex(painterList);
	      
      
      request.setAttribute("marketIndex", marketIndex);
      
      return "market_index";
    }
    
    private List<MarketIndexEntity>  getMarketIndex(List<Painter> list){
    	List<MarketIndexEntity> marketIndex = new ArrayList<MarketIndexEntity>();
    	for (Painter painter : list) {
    		String name  = painter.getName();
		 
    		String price = "";
    		String time_period = "";
    		String upDownFlag = "";
    		String upDownRatio = "";
    		Long painterId = painter.getId();
    		if(painter.getNewestIndex() !=  null){
				 price = painter.getNewestIndex().getPrice();
				 price = price.trim();
				 String price_last = painter.getNewIndex().getPrice().trim();
				 time_period = painter.getNewestIndex().getTime_period();
				 if( painter.getNewIndex() != null){
					 int price_int = 0;
					 int price_last_int = 0;
					 try {
						price_int = Integer.parseInt(price) ;
						price_last_int = Integer.parseInt(price_last);
						if(price_int > price_last_int ){
							 upDownFlag = "up";
							 upDownRatio = doubleConvertToFraction((price_int - price_last_int)/Float.parseFloat(price_last));
						}else{
							 upDownFlag = "down";
							 upDownRatio = doubleConvertToFraction((price_int - price_last_int)/Float.parseFloat(price_last));
						}
					} catch (NumberFormatException e) {
						 upDownFlag = "up";
						 upDownRatio = "0.0";
					}
				}
		 }
		 
		 if("".equals(price)){
				 continue;
		}
			 
		 MarketIndexEntity indexEntity = new MarketIndexEntity(name, price,time_period,painterId);
		 indexEntity.setUpDownFlag(upDownFlag);
		 indexEntity.setUpDownRatio(upDownRatio);
		 marketIndex.add(indexEntity);
      }
	      
	  return marketIndex;
    }

	public List<Advertisement> getAdvertisementList() {
		return advertisementList;
	}
	
	public void setAdvertisementList(List<Advertisement> advertisementList) {
		this.advertisementList = advertisementList;
	}

	public void setAdvertisementManager(AdvertisementManager advertisementManager) {
		this.advertisementManager = advertisementManager;
	}

	public void setPaintingManager(PaintingManager paintingManager) {
		this.paintingManager = paintingManager;
	}

	public void setPainterManager(PainterManager painterManager) {
		this.painterManager = painterManager;
	}
	
	private String doubleConvertToFraction(double number){
		
		 NumberFormat nf = NumberFormat.getPercentInstance();
		 nf.setMaximumFractionDigits(0);
		 
		 return nf.format(number);
	}
}

class PainterComparator implements Comparator<Painter> {
	
	public int compare(Painter o1, Painter o2) {
		
		if(o1 == null ){
			return -1;
		}
		
		if(o2 == null){
			return 1;
		}
		
		if(o1.getNewestIndex() == null){
			return -1;
		}
		
		if(o2.getNewestIndex() == null){
			return 1;
		}
		
		String price1 = o1.getNewestIndex().getPrice().trim();
		String price2 = o2.getNewestIndex().getPrice().trim();
		
		int price1_int = parsePriceToInt(price1);
		int price2_int = parsePriceToInt(price2);
		if ( price1_int > price2_int) {
			return 1;
		} else if (price1_int == price2_int) {
				return 0;
		} else if (price1_int < price2_int) {
			return -1;
		}else{
			return -1;
		}
	}
	
	int parsePriceToInt(String price){
		if(price == null || "".equals(price)){
			return 0;
		}else{
			char[] charArray = price.toCharArray();
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < charArray.length; i++) {
				if(Character.isDigit(charArray[i])){
					sb.append(charArray[i]);
				}
			}
			
			String result = sb.toString();
			if(!"".equals(result)){
				return Integer.parseInt(result);
			}else{
				return 0;
			}
		}
	}
}
